package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import ms.core.consts.SysErr;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * OperationService.
 *
 * @author zj.
 *         Created on 2020/4/8 0008.
 */
public class OperationService extends Object {
    /**
     * 运营设置：获取用户注册项配置.
     */
    public String operationSetUserGet (HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        String opertionInfo = DicParamUtils.getParamValue(DicParamEnum.GLOBAL_OPERATION_USER_REGISTER.getVal());
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, StrTool.isBlankStr(opertionInfo) ? "{}" : opertionInfo);
    }

    /**
     * 运营设置：保存用户注册项配置.
     */
    public String operationSetUserSave (HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        String user = JsonTool.getStrValue(p, "user", "");
        if (StrTool.isBlankStr(user)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        String opertionInfo = DicParamUtils.getParamValue(DicParamEnum.GLOBAL_OPERATION_USER_REGISTER.getVal());
        DicParamUtils paramUtils = new DicParamUtils();
        Map<String, Object> update = new HashedMap();
        update.put("paramValue", user);
        Integer affect = 0;
        if (StrTool.isBlankStr(opertionInfo)) {
            update.put("paramName", DicParamEnum.GLOBAL_OPERATION_USER_REGISTER.getVal());
            update.put("paramType", 1);
            affect = paramUtils.newDicParam(update);
        } else {
            Map<String, Object> where = new HashedMap();
            where.put("paramName", DicParamEnum.GLOBAL_OPERATION_USER_REGISTER.getVal());
            affect = paramUtils.modDicParam(update, where);
        }
        if (affect < 1) {
            return  XmlCodeParser.formatResultTips(false, paramUtils.getErrCode(), paramUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }
}
